package com.atguigu.eduservice.controller.front;

import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.UcenterClient;
import com.atguigu.eduservice.entity.EduComment;
import com.atguigu.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "前台系统：评论")
@RestController
@RequestMapping("/eduService/commentFront")
//@CrossOrigin
public class CommentFrontController {

    @Autowired
    private EduCommentService commentService;

    //注入UcenterClient
    @Autowired
    private UcenterClient ucenterClient;

    //1、根据课程id，分页查询评论
    @ApiOperation(value = "根据课程id分页查询评论")
    @GetMapping("/getCommentList/{courseId}/{page}/{limit}")
    public R getCommentList(@PathVariable String courseId, @PathVariable long page, @PathVariable long limit) {
        //创建Page对象
        Page<EduComment> pageComment = new Page<>(page, limit);

        //查询条件
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);

        //调用方法实现分页，把分页数据封装到pageComment对象里
        commentService.page(pageComment,wrapper);

        //总记录数
        long total = pageComment.getTotal();

        //数据list集合
        List<EduComment> records = pageComment.getRecords();

        return R.ok().data("total",total).data("records",records);
    }

    //2、根据课程id，添加评论
    @ApiOperation(value = "根据课程id，添加评论")
    @PostMapping("/addComment/{token}")
    public R addCommentByCourseId(@RequestBody(required = true) EduComment eduComment, @PathVariable String token) {
        //"1"，"小三123"：
        //eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJndWxpLXVzZXIiLCJpYXQiOjE2Mjc4OTgyMzAsImV4cCI6MTYyNzk4NDYzMCwiaWQiOiIxIiwibmlja25hbWUiOiLlsI_kuIkxMjMifQ.67HZ_mxynLMw7LbWymKcFY6RxpjP8AwLvjO68KGFwMY
        //调用服务
        R result = ucenterClient.getMemberInfoByToken(token);

        //返回数据
        Map<String, Object> data = result.getData();

        String userInfoString = String.valueOf(data.get("userInfo"));

        //字符串转转map
        String s = userInfoString.substring(1, userInfoString.length() - 1).replaceAll(" ", "");
        String[] split = s.split(",");
        HashMap<String, Object> hashMap = new HashMap<>();
        for (int i = 0; i < split.length; i++) {
            hashMap.put(split[i].split("=")[0],split[i].split("=")[1]);
        }

        //已有：评论内容、课程id、讲师id
        eduComment = eduComment;

        //用户id
        String memberId = String.valueOf(hashMap.get("id"));
        //昵称
        String nickName = String.valueOf(hashMap.get("nickname"));
        //用户头像地址
        String avatar = String.valueOf(hashMap.get("avatar"));

        //设置值
        eduComment.setMemberId(memberId);
        eduComment.setNickname(nickName);
        eduComment.setAvatar(avatar);

        //数据库添加评论
        commentService.save(eduComment);

        return R.ok();
    }

}
